package com.prp.gwt.client.widgets.user.php;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.vo.SubjectRecommendList;

public class RecentRecommendPanel extends DecoratorPanel {

	private static RecentRecommendPanel instance;
	private VerticalPanel innerPanel = new VerticalPanel();
	private VerticalPanel vPanel = new VerticalPanel();
	private Image loadImage = new Image(GWT.getModuleBaseURL() + "loading.gif");
	private SubjectRecommendList recommendList;
	public static final int NUM_PER_PAGE = 8;
	private int index;
	HTML head = new HTML("<a>首页</a>");
	HTML next = new HTML("<a>下一页</a>");
	HTML last = new HTML("<a>上一页</a>");
	HTML tail = new HTML("<a>尾页</a>");
	HTML title;
	
	public static RecentRecommendPanel getInstance() {
		return instance;
	}

	public RecentRecommendPanel() {
		instance = this;
		addStyleName("recentRecommend");
		title = new HTML("主题贴");
		title.addStyleName("recentRecommend-title");
		innerPanel.add(title);
		innerPanel.add(loadImage);
		innerPanel.add(vPanel);
		vPanel.addStyleName("page-frame");
		add(innerPanel);
		head.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(1);
			}
		});
		tail.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(getPageCount());
			}
		});
		next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index + 1);
			}
		});
		last.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				toPage(index - 1);
			}
		});
	}

	public void init(){
		vPanel.clear();
		loadImage.setVisible(true);
		title.setText("主题贴");
	}
	
	public void load(SubjectRecommendList list){
		loadImage.setVisible(false);
		recommendList = list;
		if(list.getOwner() == null)
			title.setText("我的主题贴");
		else
			title.setText(list.getOwner().getUsername()+"的主题贴");
		toPage(1);
	}
	
	public void toPage(int index) {
		if (index > 0 && index <= getPageCount())
			this.index = index;
		else
			return;
		vPanel.clear();
		loadPagePanel();
		loadRecommendItem();
	}
	
	public void loadRecommendItem() {
		int start = (index - 1) * NUM_PER_PAGE;
		int end = Math.min(index * NUM_PER_PAGE, recommendList.size());
		for (int i = start; i < end; i++)
			vPanel.add(new RecentRecommendItem(recommendList.get(i)));
	}

	private void loadPagePanel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(5);
		int start = 0;
		int end = 0;
		if (getPageCount() <= 5 || index < 3) {
			start = 1;
			end = Math.min(5, getPageCount());
		} else if (getPageCount() - index < 3) {
			start = getPageCount() - 4;
			end = getPageCount();
		} else {
			start = index - 2;
			end = index + 2;
		}
		panel.add(head);
		panel.add(last);
		if (index == 1) {
			head.removeStyleName("page");
			last.removeStyleName("page");
			head.addStyleName("page-disable");
			last.addStyleName("page-disable");
		} else {
			head.removeStyleName("page-disable");
			last.removeStyleName("page-disable");
			head.addStyleName("page");
			last.addStyleName("page");
		}
		for (int i = start; i <= end; i++) {
			if (i == index) {
				HTML l = new HTML("<a>" + String.valueOf(i) + "</a>");
				l.addStyleName("page-selected");
				panel.add(l);
				continue;
			}
			HTML label = new HTML("<a>" + String.valueOf(i) + "</a>");
			label.setStylePrimaryName("page");
			label.setStylePrimaryName("page");
			panel.add(label);
			final int j = i;
			label.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					toPage(j);
				}
			});
		}
		panel.add(next);
		panel.add(tail);
		if (index == getPageCount()) {
			next.removeStyleName("page");
			tail.removeStyleName("page");
			next.addStyleName("page-disable");
			tail.addStyleName("page-disable");
		} else {
			next.removeStyleName("page-disable");
			tail.removeStyleName("page-disable");
			next.addStyleName("page");
			tail.addStyleName("page");
		}
		vPanel.add(panel);
		vPanel.setCellHorizontalAlignment(panel, HasHorizontalAlignment.ALIGN_RIGHT);
	}

	public int getPageCount() {
		return (recommendList.size() - 1) / NUM_PER_PAGE + 1;
	}

}
